<?php
namespace app\home\model;
use think\Model;
use think\Db;
use think\Session;
use think\Exception;

class Paper extends model
{
    protected $error = '';
    /**
     * 返回试卷列表
     * @access public
     * @return array
     */
    public function getList(){
        //查询已经发布的试卷
        $data = Db::table('paper')
            ->where('is_publish',1)
            ->field('id,title,allscore,timelimit,singlenum,mulnum,fillnum,shortnum,judgenum,idenum')
            ->select();
        return $data;
    }
    /**
     * 返回试卷所有信息
     * @access public
     * @return array
     */
    public function getAllQusetion($id){
        $this->error = '';
        if($id){
            $data = Db::table('paper')
                ->alias('a')
                ->join('paperdetail w','a.id = w.paperid')
                ->where('a.id',$id)
                ->field('a.title,a.allscore,a.timelimit,a.singlenum,a.mulnum,a.fillnum,a.shortnum,a.judgenum,a.idenum,w.single,w.mul,w.ide,w.fill,w.judge,w.short')
                ->find();
        }else{
            $this->error = '参数错误';
        }
        return empty($error) ? $data : false;
    }
    /**
     * 获取错误信息
     */
    public function getError(){
        return $this->error;
    }

    /**
     * 批阅试卷 把考试结果写入数据表
     * @access public
     * @param $examdetail 做题结果
     * @param $paperid 试卷id
     * @param $stuid 学生id
     * @return bool
     */
    public function saveExamResult($examdetail,$paperid,$stuid,$stu_answer){
        $this->error = '';
        $paperids = '';
        //更新examresult 表  更新Student表
        Db::startTrans();
        try{
            $re = Db::table('examresult')
                ->insert($examdetail);
            if($re){
                $res = Db::table('student')
                    ->where('stuid',$stuid)
                    ->field('paperid')
                    ->find();
                if($res['paperid']){
                    $paperids = $res['paperid'].','.$paperid;
                }else{
                    $paperids = $paperid;
                }
                $ress = Db::table('student')
                    ->where('stuid',$stuid)
                    ->update(['paperid' => $paperids]);
                if(!$ress){
                    throw new Exception('错误');
                }else{
                    //添加学生选的答案
                    if($stu_answer && is_array($stu_answer)){
                        foreach($stu_answer as $ks=>$vs){
                            switch($ks){
                                case 'single';
                                    $stu_answers = [];
                                    foreach($vs as $ks1=>$vs1){
                                        $stu_answers['stuid'] = $stuid;
                                        $stu_answers['paperid'] = $paperid;
                                        $stu_answers['type'] = 'single';
                                        $stu_answers['problemid'] = $ks1;
                                        $stu_answers['answer'] = $vs1;
                                        $infnum = Db::table('stu_answer')->insert($stu_answers);
                                        if(!$infnum){
                                            $this->error = 'single error';
                                        }
                                    }
                                    break;
                                case 'mul';
                                    $stu_answers = [];
                                    foreach($vs as $ks1=>$vs1){
                                        $stu_answers['stuid'] = $stuid;
                                        $stu_answers['paperid'] = $paperid;
                                        $stu_answers['type'] = 'mul';
                                        $stu_answers['problemid'] = $ks1;
                                        $stu_answers['answer'] = $vs1;
                                        $infnum = Db::table('stu_answer')->insert($stu_answers);
                                        if(!$infnum){
                                            $this->error = 'mul error';
                                        }
                                    }
                                    break;
                                case 'ide';
                                    $stu_answers = [];
                                    foreach($vs as $ks1=>$vs1){
                                        $stu_answers['stuid'] = $stuid;
                                        $stu_answers['paperid'] = $paperid;
                                        $stu_answers['type'] = 'ide';
                                        $stu_answers['problemid'] = $ks1;
                                        $stu_answers['answer'] = $vs1;
                                        $infnum = Db::table('stu_answer')->insert($stu_answers);
                                        if(!$infnum){
                                            $this->error = 'ide error';
                                        }
                                    }
                                    break;
                                case 'fill';
                                    $stu_answers = [];
                                    foreach($vs as $ks1=>$vs1){
                                        $stu_answers['stuid'] = $stuid;
                                        $stu_answers['paperid'] = $paperid;
                                        $stu_answers['type'] = 'fill';
                                        $stu_answers['problemid'] = $ks1;
                                        $stu_answers['answer'] = $vs1;
                                        $infnum = Db::table('stu_answer')->insert($stu_answers);
                                        if(!$infnum){
                                            $this->error = 'fill error';
                                        }
                                    }
                                    break;
                                case 'judge';
                                    $stu_answers = [];
                                    foreach($vs as $ks1=>$vs1){
                                        $stu_answers['stuid'] = $stuid;
                                        $stu_answers['paperid'] = $paperid;
                                        $stu_answers['type'] = 'judge';
                                        $stu_answers['problemid'] = $ks1;
                                        $stu_answers['answer'] = $vs1;
                                        $infnum = Db::table('stu_answer')->insert($stu_answers);
                                        if(!$infnum){
                                            $this->error = 'judge error';
                                        }
                                    }
                                    break;
                                case 'short';
                                    $stu_answers = [];
                                    foreach($vs as $ks1=>$vs1){
                                        $stu_answers['stuid'] = $stuid;
                                        $stu_answers['paperid'] = $paperid;
                                        $stu_answers['type'] = 'short';
                                        $stu_answers['problemid'] = $ks1;
                                        $stu_answers['answer'] = $vs1;
                                        $infnum = Db::table('stu_answer')->insert($stu_answers);
                                        if(!$infnum){
                                            $this->error = 'short error';
                                        }
                                    }
                                    break;
                            }
                        }
                        if($this->error){
                            throw new Exception('错误');
                        }else{
                            // 提交事务
                            Db::commit();
                        }
                    }else{
                        throw new Exception('参数空');
                    }
                }
            }else{
                throw new Exception('错误');
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error = $e->getMessage();
        }
        return empty($this->error) ? true : false;
//        return  $this->error;
    }
}
